// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package copyengine.utils.effect.ghostShadow
{
	import com.greensock.TweenMax;

	import starling.display.DisplayObjectContainer;
	import starling.display.Image;
	import starling.textures.Texture;

	public final class CEGhostShadowSpriteFragment
	{
		private var mFragmentLiftTime:Number;
		private var mFinishCallback:Function;
		private var mFragmentImage:Image;
		private var spTexture:Texture;
		private var mParentContainer:DisplayObjectContainer;

		private var mIsDisposed:Boolean;

		public function CEGhostShadowSpriteFragment()
		{
		}

		//=================================
		//== Initianlize & Dispose
		//=================================
		public function initialize(_spTexture:Texture, _parentContainer:DisplayObjectContainer,
								   _initPosX:Number, _initPosY:Number,
								   _fragmentLiftTime:Number,
								   _finishCallback:Function):void
		{
			spTexture=_spTexture;
			mParentContainer=_parentContainer;
			mFinishCallback=_finishCallback;
			mFragmentLiftTime=_fragmentLiftTime;

			mFragmentImage=new Image(spTexture);
			mParentContainer.addChild(mFragmentImage);

			mFragmentImage.x=_initPosX;
			mFragmentImage.y=_initPosY;

			TweenMax.to(mFragmentImage, mFragmentLiftTime, {alpha: 0, onComplete: onFragmentLiftTimeComplate})
		}

		public function dispoise():void
		{
			if (!mIsDisposed)
			{
				TweenMax.killTweensOf(this);
				mFinishCallback=null;
				mFragmentImage.removeFromParent(true);
				mFragmentImage=null;
				mIsDisposed=true;
			}
		}

		//=================================
		//== Publish Function
		//=================================
		public function reuse(_initPosX:Number, _initPosY:Number):void
		{
			mFragmentImage.visible=true;
			mFragmentImage.alpha=1;
			mParentContainer.addChild(mFragmentImage);
			mFragmentImage.x=_initPosX;
			mFragmentImage.y=_initPosY;
			TweenMax.to(mFragmentImage, mFragmentLiftTime, {alpha: 0, onComplete: onFragmentLiftTimeComplate})
		}

		public function recycle():void
		{
			mFragmentImage.removeFromParent();
			mFragmentImage.visible=false;
		}


		//================================
		//== Private Function
		//================================

		private function onFragmentLiftTimeComplate():void
		{
			mFinishCallback && mFinishCallback(this);
		}


	}
}
